package com.njwb.day14.dao.service;

import com.njwb.day14.dao.StudentDao;
import com.njwb.day14.dao.impl.StudentDaoImpl;
import com.njwb.day14.dao.pojo.Student;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class StudentManager {
    public static void main(String[] args) {
        showMenu();
    }
    public static void showMenu(){

        Scanner scanner = new Scanner(System.in);
        StudentDao studentDao = new StudentDaoImpl();
        do{
            System.out.println("-----------------------------");
            System.out.println("1、添加学生");
            System.out.println("2、修改学生");
            System.out.println("3、删除学生");
            System.out.println("4、查询所有学生");
            System.out.println("5、退出");
            System.out.println("-----------------------------");
            System.out.println("请选择：");
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:

                    System.out.println("请输入姓名：");
                    String name = scanner.next();
                    System.out.println("请输入年龄：");
                    int age = scanner.nextInt() ;
                    System.out.println("请输入住址：");
                    String address=scanner.next();
                    Student student= new Student(name,age,address);
                    try {
                        studentDao.addStudent(student);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    continue;
                case 2:
                    Scanner input= new Scanner(System.in);
                    System.out.println("请输入要修改的学生编号：");
                    //提供要更新数据编号
                    int sid= input.nextInt();
                    try {
                        Student s = studentDao.findStudentById(sid);
                        if(s==null) {
                            System.out.println("数据不存在");

                        }else{
                            System.out.println("找到了编号为6的数据："+ s.toString());
                            //下面值是最终6号的数据
                            System.out.println("请输入新的姓名");
                            s.setSname(input.next());
                            System.out.println("请输入新的年龄");
                            s.setAge(input.nextInt());
                            System.out.println("请输入新的地址");
                            s.setAddress(input.next());

                            int result = studentDao.updateStudent(s);

                            if(result>0) {
                                System.out.println("数据更新成功");
                            }
                        }

                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    continue;
                case 3:
                    System.out.println("请输入要删除的学生编号：");
                    int deleteId = scanner.nextInt();

                    try {
                        if(studentDao.deleteStudentById(deleteId)>0) {
                            System.out.println("数据删除成功！！！");
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    continue;
                case 4:
                    try {
                        List<Student> studentList = studentDao.queryAllStudents();
                        for(Student s:studentList) {
                            System.out.println(s);
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    continue;
                case 5:
                default:
                    break;
            }
            break;

        }while(true);
    }
}
